
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月30日 上午10:59:50    
* 修改时间：2018年6月30日 上午10:59:50   
* 修改备注：   
* @version    
*    
*/
package com.yongan.production.service.impl;

import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.yongan.production.dao.ProductStructureListDao;
import com.yongan.production.entity.Details;
import com.yongan.production.entity.ProductStructureList;
import com.yongan.production.service.DetailsService;
import com.yongan.production.service.ProductStructureListService;
import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;
import com.yongan.warehouse.dao.GoodsinformationDao;

/**
 * @author liubo
 *
 */
@Service
public class ProductStructureListServiceImpl implements ProductStructureListService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ProductStructureListDao productStructureListDao;
	@Autowired
	private DetailsService detailsService;
	@Autowired
	private GoodsinformationDao goodsinformationDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	@Override
	public List<ProductStructureList> getProductStructureList(int offset, int limit) {
		List<ProductStructureList> list = productStructureListDao.queryAll(offset, limit);
		for(ProductStructureList productStructureList:list){
			productStructureList.setDetails(detailsService.getDetailsList(productStructureList.getProductStructureListId()));
			productStructureList.setGoodsinformation(goodsinformationDao.queryById(productStructureList.getGoodsinformationId()));
		}
		return list;
	}

	
	@Override
	public ProductStructureList getProductStructureListById(String productStructureListId) {
		ProductStructureList productStructureList = productStructureListDao.queryById(productStructureListId);
		productStructureList.setGoodsinformation(goodsinformationDao.queryById(productStructureList.getGoodsinformationId()));
		productStructureList.setDetails(detailsService.getDetailsList(productStructureListId));
		return productStructureList;
	}
	@Override
	public ProductStructureList getProductStructureListByGoodsId(String productGoodsId) {
		ProductStructureList productStructureList = productStructureListDao.queryByGoodsId(productGoodsId);
		if(productStructureList!=null){
			productStructureList.setDetails(detailsService.getDetailsList(productStructureList.getProductStructureListId()));
			productStructureList.setGoodsinformation(goodsinformationDao.queryById(productStructureList.getGoodsinformationId()));
		}
		return productStructureList;
	}

	@Override
	@Transactional
	public Execution executeProductStructureList(ProductStructureList productStructureList,
			String productStructureListId, String md5) throws UserException {
		if(md5 == null || !md5.equals(getMD5(productStructureListId))){
			//throw new UserException("user data rewrite");
		}
		List<Details> details =null;
		try{
			if(productStructureList == null){
				ProductStructureList readDell = productStructureListDao.queryById(productStructureListId);
				details = readDell.getDetails();
				int deleteCount = productStructureListDao.deleteProductStructureList(productStructureListId);
				int deleteDetailsCount = detailsService.deleteAllByTopId(productStructureListId);
				if(deleteCount<=0 & deleteDetailsCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(productStructureListId, UserStatEnum.SUCCESS);
				}
			}else if((productStructureListId.equals(""))){
				details = productStructureList.getDetails();
				int goodDetailsaddcount = 1;
				String topid = UUID.randomUUID().toString();
				if(!(details==null)){
					for(Details goodDetails:details){
						goodDetails.setDetailsId(UUID.randomUUID().toString());;
						goodDetails.setTopId(topid);
						int addInGoodDetailsCount = detailsService.addDetails(goodDetails);
						if(addInGoodDetailsCount<=0){
							goodDetailsaddcount = addInGoodDetailsCount;
						}
					}
				}
				productStructureList.setProductStructureListId(topid);
				int addCount = productStructureListDao.insertProductStructureList(productStructureList);
				if(addCount<=0 & goodDetailsaddcount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(productStructureList.getProductStructureListId(), UserStatEnum.SUCCESS);
				}
			}else if(!(productStructureListId.equals(""))){
				//List<Details> oldList = detailsService.getDetailsList(productStructureListId);
				int dellAll = detailsService.deleteAllByTopId(productStructureListId);
				//List<String> newList = new ArrayList<String>();
				details = productStructureList.getDetails();
				System.out.println(details);
				int goodDetailscount = 1;
				if(!(details==null)){
					for(Details goodDetails:details){
						goodDetails.setDetailsId(UUID.randomUUID().toString());
						goodDetails.setTopId(productStructureListId);
						int ua = detailsService.addDetails(goodDetails);
						if(ua<=0){
							goodDetailscount = ua;
						}
						
					}
				}
				int updateCount = productStructureListDao.updateProductStructureList(productStructureList);
				if(updateCount<=0 & goodDetailscount<=0 & dellAll<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(productStructureListId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	
	@Override
	public int getCount() {
		// TODO 自动生成的方法存根
		return productStructureListDao.queryCount();
	}
	
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}


	



	
}
